package com.shigu.admin.action;

import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.exceptions.AdminException;
import com.shigu.postFee.bo.*;
import com.shigu.postFee.enums.PostFeeType;
import com.shigu.postFee.service.PostFeeService;
import com.shigu.postFee.vo.FeeModelVo;
import com.shigu.postFee.vo.PostModelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("/adminjson")
public class PostFeeModelController {
    @Autowired
    PostFeeService postFeeService;

    @RequestMapping("postFeeModel")
    @ResponseBody
    public PageResponse<PostModelVo> postFeeModel() {
        return postFeeService.postFeeModel();
    }

    @RequestMapping("addPostFeeModel")
    @ResponseBody
    public MallResponse addPostFeeModel(@Valid AddPostModelRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }

        AddPostModelBo bo = new AddPostModelBo();
        if (request.getType() != PostFeeType.FREE.getValue()) {
            bo.setFee(Double.valueOf(request.getFee()));
            bo.setLimit(request.getLimit());
            bo.setIncrease(request.getIncrease());
            bo.setIncreaseFee(Double.valueOf(request.getIncreaseFee()));
        }
        bo.setName(request.getName());
        bo.setType(PostFeeType.selByValue(request.getType()));
        if (request.getSonType() != null) {
            bo.setSonType(PostFeeType.selByValue(request.getSonType()));
            bo.setProvIds(request.getProvIds());
            bo.setSonFee(Double.valueOf(request.getSonFee()));
            bo.setSonLimit(request.getSonLimit());
            bo.setSonIncrease(request.getSonIncrease());
            bo.setSonIncreaseFee(Double.valueOf(request.getSonIncreaseFee()));
        }
        return postFeeService.addPostFeeModel(bo);
    }

    @RequestMapping("postModelList")
    @ResponseBody
    public PageResponse<FeeModelVo> postModelList() {
        return postFeeService.postModelList();
    }

    @RequestMapping("deletePostModel")
    @ResponseBody
    public MallResponse deletePostModel(@Valid FeeIdRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        return postFeeService.deletePostModel(request.getFeeId());
    }

    @RequestMapping("modifyPostFeeModel")
    @ResponseBody
    public MallResponse modifyPostFeeModel(@Valid ModifyPostModelRequest request, BindingResult result) throws AdminException {
        if (result.hasErrors()) {
            throw new AdminException(result.getFieldError().getDefaultMessage());
        }
        ModifyPostModelBo bo = new ModifyPostModelBo();

        bo.setFeeId(request.getFeeId());
        bo.setName(request.getName());
        bo.setType(PostFeeType.selByValue(request.getType()));
        if (request.getType() != PostFeeType.FREE.getValue()) {
            bo.setFee(Double.valueOf(request.getFee()));
            bo.setLimit(request.getLimit());
            bo.setIncrease(request.getIncrease());
            bo.setIncreaseFee(Double.valueOf(request.getIncreaseFee()));
        }
        if (request.getSonType() != null) {
            bo.setSonType(PostFeeType.selByValue(request.getSonType()));
            bo.setProvIds(request.getProvIds());
            bo.setSonFee(Double.valueOf(request.getSonFee()));
            bo.setSonLimit(request.getSonLimit());
            bo.setSonIncrease(request.getSonIncrease());
            bo.setSonIncreaseFee(Double.valueOf(request.getSonIncreaseFee()));
        }
        return postFeeService.modifyPostFeeModel(bo);
    }
}
